package com.cloorc.example.cloud.service;

import com.cloorc.example.clouds.api.ProviderV1Api;
import com.cloorc.example.clouds.api.WhereWho;
import com.cloorc.example.clouds.api.Who;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.security.SecureRandom;
import java.util.Collections;
import java.util.List;

@Service
public class ProviderV1 implements ProviderV1Api {
    private static final SecureRandom SECURE_RANDOM = new SecureRandom();

    @Override
    public ResponseEntity<List<String>> hello(final WhereWho whereWho) {
        Who in = whereWho.getWho();
        if (null == in)
            in = new Who("Spring Cloud", 24);
        try {
            Thread.sleep((long) (SECURE_RANDOM.nextInt(1000)));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return new ResponseEntity<>(
            Collections.singletonList("{\"msg\":\"Hello, " + in.getName().substring(0, 1).toUpperCase() + in.getName().substring(1) + "!\",\"age\":\"" + in.getAge() + "\"}"),
            HttpStatus.OK);
    }
}
